package cn.com.ctcloud.saas.user.webtier;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cn.com.ctcloud.saas.core.utils.Log;
import cn.com.ctcloud.saas.user.service.IUserService;

/**
 * 正常Controller
 */
@Controller // 使用@Controller 注解，在对应的方法上，视图解析器可以解析return 的jsp,html页面，并且跳转到相应页面，若返回json等内容到页面，则需要加@ResponseBody注解
@RequestMapping("/user")
public class UserWebController {
	@Autowired
	IUserService userService;

	/**
	 * 访问地址：http://localhost:9090/saas/user/initLogin.xhtml
	 */
	@RequestMapping("/initLogin.xhtml")
	public ModelAndView initLogin() {
		ModelAndView mav = new ModelAndView("login/login");
		Log.info("initLogin");
		return mav;
	}

	/**
	 * 访问地址：http://localhost:9090/saas/user/searchUser.xhtml
	 */
	@RequestMapping("/searchUser.xhtml")
	public ModelAndView searchUser() {
		// 页面位置 /WEB-INF/jsp/user/list.jsp
		ModelAndView mav = new ModelAndView("user/list");
		mav.addObject("users", userService.findAll());
		Log.info("searchUser");
		return mav;
	}

	//处理文件上传
	@RequestMapping(value = "/uploadimg", method = RequestMethod.POST)
	public @ResponseBody String uploadImg(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
		System.out.println("调用文件上传方法");
		String contentType = file.getContentType();
		String fileName = file.getOriginalFilename();
		return fileName;
	}
}
